package com.haixiaoke.saas.house.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.haixiaoke.saas.common.exception.ServiceException;
import com.haixiaoke.saas.common.utils.DateUtils;
import com.haixiaoke.saas.common.utils.TreeUtil.TreeUtil;
import com.haixiaoke.saas.house.domain.CleaningProcess;
import com.haixiaoke.saas.house.domain.CleaningProcessTree;
import com.haixiaoke.saas.house.domain.House;
import com.haixiaoke.saas.house.mapper.CleaningProcessMapper;
import com.haixiaoke.saas.house.service.ICleaningProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 保洁流程Service业务层处理
 *
 * @author panda
 * @date 2024-05-09
 */
@Service
public class CleaningProcessServiceImpl implements ICleaningProcessService {
    @Autowired
    private CleaningProcessMapper cleaningProcessMapper;

    /**
     * 查询保洁流程
     *
     * @param id 保洁流程主键
     * @return 保洁流程
     */
    @Override
    public CleaningProcess selectCleaningProcessById(Long id) {
        return cleaningProcessMapper.selectCleaningProcessById(id);
    }

    /**
     * 查询保洁流程列表
     *
     * @param cleaningProcess 保洁流程
     * @return 保洁流程
     */
    @Override
    public List<CleaningProcess> selectCleaningProcessList(CleaningProcess cleaningProcess) {
        return cleaningProcessMapper.selectCleaningProcessList(cleaningProcess);
    }

    /**
     * 新增保洁流程
     *
     * @param cleaningProcess 保洁流程
     * @return 结果
     */
    @Override
    public int insertCleaningProcess(CleaningProcess cleaningProcess) {
        cleaningProcess.setCreateTime(DateUtils.getNowDate());
        return cleaningProcessMapper.insertCleaningProcess(cleaningProcess);
    }

    /**
     * 修改保洁流程
     *
     * @param cleaningProcess 保洁流程
     * @return 结果
     */
    @Override
    public int updateCleaningProcess(CleaningProcess cleaningProcess) {
        cleaningProcess.setUpdateTime(DateUtils.getNowDate());
        return cleaningProcessMapper.updateCleaningProcess(cleaningProcess);
    }

    /**
     * 批量软删除保洁流程
     *
     * @param ids 需要删除的保洁流程主键
     * @return 结果
     */
    @Override
    public int deleteCleaningProcessByIds(Long[] ids) {
        return cleaningProcessMapper.deleteCleaningProcessByIds(ids);
    }

    /**
     * 删除软保洁流程信息
     *
     * @param id 保洁流程主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteCleaningProcessById(Long id, String tenantId) {
        CleaningProcess cleaningProcess = cleaningProcessMapper.selectCleaningProcessById(id);
        if (cleaningProcess.getParentId() == null) {
            int resultPlace = this.countCleaningProcessByPlace(tenantId,cleaningProcess.getHouseId());
            if (resultPlace <= 1) {
                throw new ServiceException("保洁位置不能小于1个");
            }

            int resultCheckPoint = this.countCleaningProcessByCheckPoint(tenantId, cleaningProcess.getId(), cleaningProcess.getHouseId());
            if (resultCheckPoint >0) {
                throw new ServiceException("位置下有检查点，不能删除");
            }
        }
        int result = cleaningProcessMapper.deleteCleaningProcessById(id);
        if (result >0) {
            cleaningProcessMapper.updateCleaningProcessBySort(cleaningProcess.getTenantId(), cleaningProcess.getParentId(), cleaningProcess.getSort());
        }
        return result;
    }

    @Override
    public int insertCleaningProcessByPlace(CleaningProcess cleaningProcess) {
        int result = this.countCleaningProcessByPlace(cleaningProcess.getTenantId(),cleaningProcess.getHouseId());
        cleaningProcess.setSort(result + 1);
        cleaningProcess.setCreateTime(DateUtils.getNowDate());
        return cleaningProcessMapper.insertCleaningProcess(cleaningProcess);
    }

    @Override
    public int insertCleaningProcessByCheckPoint(CleaningProcess cleaningProcess) {
        int result = this.countCleaningProcessByCheckPoint(cleaningProcess.getTenantId(), cleaningProcess.getParentId(),cleaningProcess.getHouseId());
        cleaningProcess.setSort(result + 1);
        cleaningProcess.setCreateTime(DateUtils.getNowDate());
        return cleaningProcessMapper.insertCleaningProcess(cleaningProcess);
    }

    @Override
    public int countCleaningProcessByPlace(String tenantId,String houseId) {
        return cleaningProcessMapper.countCleaningProcessByPlace(tenantId,houseId);
    }

    @Override
    public int countCleaningProcessByCheckPoint(String tenantId, Long parentId,String houseId) {
        return cleaningProcessMapper.countCleaningProcessByCheckPoint(tenantId, parentId,houseId);
    }

    @Override
    public int cleaningProcessMoveUp(Long id, String tenantId) {
        CleaningProcess cleaningProcess = cleaningProcessMapper.selectCleaningProcessById(id);
        int sort = cleaningProcess.getSort();
        CleaningProcess beforeCleaningProcess = cleaningProcessMapper.selectCleaningProcessBySort(tenantId, cleaningProcess.getParentId(),cleaningProcess.getHouseId(), sort - 1);
        if (ObjectUtil.isEmpty(beforeCleaningProcess)) {
            throw new ServiceException("上移失败，已到顶");
        }
        int beforeSort = beforeCleaningProcess.getSort();
        cleaningProcess.setSort(beforeSort);
        beforeCleaningProcess.setSort(sort);
//        cleaningProcessMapper.updateCleaningProcess(cleaningProcess);
//        cleaningProcessMapper.updateCleaningProcess(beforeCleaningProcess);
        List<CleaningProcess> cleaningProcessList = Arrays.asList(cleaningProcess, beforeCleaningProcess);
        return cleaningProcessMapper.updateBatchCleaningProcess(cleaningProcessList);
    }

    @Override
    public int cleaningProcessShiftDown(Long id, String tenantId) {
        CleaningProcess cleaningProcess = cleaningProcessMapper.selectCleaningProcessById(id);
        int sort = cleaningProcess.getSort();
        CleaningProcess beforeCleaningProcess = cleaningProcessMapper.selectCleaningProcessBySort(tenantId, cleaningProcess.getParentId(),cleaningProcess.getHouseId(), sort + 1);
        if (ObjectUtil.isEmpty(beforeCleaningProcess)) {
            throw new ServiceException("下移失败，已到底");
        }
        int beforeSort = beforeCleaningProcess.getSort();
        cleaningProcess.setSort(beforeSort);
        beforeCleaningProcess.setSort(sort);
//        cleaningProcessMapper.updateCleaningProcess(cleaningProcess);
//        cleaningProcessMapper.updateCleaningProcess(beforeCleaningProcess);
        List<CleaningProcess> cleaningProcessList = Arrays.asList(cleaningProcess, beforeCleaningProcess);
        return cleaningProcessMapper.updateBatchCleaningProcess(cleaningProcessList);
    }

    @Override
    public List<CleaningProcessTree> selectCleaningProcessTree(CleaningProcess cleaningProcess) {
        List<CleaningProcessTree> cleaningProcessList = cleaningProcessMapper.selectCleaningProcessTree(cleaningProcess);
        return TreeUtil.generateTree(cleaningProcessList);
    }

    @Override
    public List<CleaningProcess> selectHouseListByCleaningProcess(CleaningProcess cleaningProcess) {
        return cleaningProcessMapper.selectHouseListByCleaningProcess(cleaningProcess);
    }

    @Override
    public List<CleaningProcess> selectCleaningProcessListAndParentId(CleaningProcess cleaningProcess) {
        return cleaningProcessMapper.selectCleaningProcessListAndParentId(cleaningProcess);
    }
}
